package edu.controller.impl;

import edu.controller.PersonController;
import edu.service.impl.BaseServiceImpl;

import java.util.ArrayList;

public abstract class BasePersonController<E> implements PersonController<E> {


    BaseServiceImpl<E> personService = new BaseServiceImpl<>();
    @Override
    public void start(String msg) {
        System.out.println("欢迎登录"+ msg +"管理系统");
        s:while (true) {
            System.out.println("请选择你要执行的操作");
            System.out.println("1.添加"+ msg +"  2.移除"+ msg +"  3.修改"+msg +"  4.查看"+msg +"  5.返回上级");
            switch (sc.nextLine()) {
                case "1":
                    addPerson(msg);
                    break;
                case "2":
                    delectPerson(msg);
                    break;
                case "3":
                    updatePerson(msg);
                    break;
                case "4":
                    findAllPerson(msg);
                    break;
                case "5":
                    System.out.println("感谢使用,再见");
                    break s;
                default:
                    System.out.println("请输入正确指令");
                    System.out.println();

            }
        }
    }

    @Override
    public void findAllPerson(String msg) {
        ArrayList<E> allStudent = personService.findAllPerson();
        if (allStudent.size() == 0) {
            System.out.println("当前系统无数据,请先添加");
        }else{
            System.out.println("id \t name \t birthday \t\t age");
            for (E e : allStudent) {
                System.out.println(e.toString());
            }
        }
    }

    @Override
    public void delectPerson(String msg) {
        System.out.println("请输入删除的" + msg + "编号:");
        String id = sc.nextLine();
        int index = personService.getIndexById(id);
        if (index == -1){
            System.out.println("当前编号不存在,无需删除");
        }else {
            personService.delectPerson(index);
            System.out.println("删除成功");
        }
    }


}
